<?php
/*
 * 微信
 */
namespace app\admin\controller;
use app\common\controller\AdminBase;
use app\common\model\Wechat as WechatModel;
use think\facade\Db;

class Wechat extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }
    public function index()
    {
        $this->fans();
    }
    //微信用户
    public function fans()
    {
        $list = Db::table('wechat')->order('create_time','desc')->paginate();
        return $this->fetch('index', ['list' => $list]);
    }
    //获取用户
    public function update_user()
    {
        $app = WechatModel::wechat();
        $users = $app->user->list();
        foreach ($users['data']['openid'] as $r){
            $wechat = Db::table('wechat')->where(['openid'=>$r])->find();
            if(!$wechat){
                $user = $app->user->get($r);
                if(!isset($user['unionid'])){
                    $user['unionid'] = '0';
                }
                //创建公众号资料
                WechatModel::create([
                    'openid'         => $r,
                    'nickname'       => $user['nickname'],
                    'sex'            => $user['sex'],
                    'country'        => $user['country'],
                    'province'       => $user['province'],
                    'city'           => $user['city'],
                    'headimgurl'     => $user['headimgurl'],
                    'unionid'        => $user['unionid'],
                    'subscribe'      => $user['subscribe'],
                    'subscribe_time' => $user['subscribe_time'],
                    'subscribe_scene'=> $user['subscribe_scene'],
                ]);
            }
        }
        $this->success('获取成功');
    }
    //消息推送
    public function push()
    {
    	if ($this->request->isPost()) {
            $param = $this->request->param();
            $verify = input('_verify', true); 
            if($verify!='0'){
                try{
                    $this->validate($param, 'push');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $param['sex']  == -1 && $param['sex'] = '0,1,2';
            $data = Db::table('wechat')->where('subscribe',1)->where('sex','in',$param['sex'])->column('openid');
            !$data && $this->error('用户不能为空！');
            $text = trim($param['content']);
            // 推送消息
            $result = WechatModel::weixinSendText($text,$data);//处理消息推送
            !$result && $this->error($this->errorMsg);
            insert_admin_log('推送了微信消息');
            $this->success('推送成功');
        }
        return $this->fetch('push');
    }
    //自定义菜单
    public function menu()
    {
        $app = WechatModel::wechat();
        $list = $app->menu->current();
        return $this->fetch('menu',['list'=>$list]);
    }
    //添加菜单
    public function add_menu()
    {
        if ($this->request->isPost()) {
            $buttons = $this->request->param('menu');
            $app = WechatModel::wechat();
            $save = $app->menu->create($buttons['button']);
            if ($save['errcode']) {
                echo json_encode(['errcode'=>$save['errcode'],'msg'=>$save['errmsg']]);exit;
            }
            echo json_encode(['errcode'=>0,'msg'=>'更新成功']);exit;
        }
    }

}